<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>代码生成器</title>
    <div th:replace="common/link::header"></div>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <form class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="codeConfigForm" id="codeConfigForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="author" lay-verify="required" placeholder="请输入作者" class="layui-input" value="yangyouwang">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">模块名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="moduleName" lay-verify="required" placeholder="请输入模块名" class="layui-input" value="system">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">表名</label>
                    <div class="layui-input-inline">
                        <select name="tableName" id="tableName" lay-verify="required" lay-search lay-filter="tableName">
                            <option value=""></option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">菜单</label>
                    <div class="layui-input-inline">
                        <div class="layui-row">
                            <div class="layui-col-md9">
                                <input name="menuId" type="hidden" value="0">
                                <input name="menuName" type="text" class="layui-input layui-disabled" placeholder="顶级" disabled>
                            </div>
                            <div class="layui-col-md3">
                                <button type="button" class="layui-btn layui-btn-primary menuWizard">选择</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <button type="button" class="layui-btn" lay-submit lay-filter="save-submit" id="save-submit">生成代码</button>
                    </div>
                </div>
            </div>
        </form>
        <div class="layui-card-body">
            <table id="fieldList" lay-filter="fieldList"></table>
        </div>
    </div>
</div>
<div th:replace="common/script::footer"></div>
<script th:inline="javascript">
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index', //主入口模块
    }).use(['index', 'form','crud', 'table'], function(){
        let $ = layui.$,
            form = layui.form,
            crud = layui.crud,
            table = layui.table,
            codeConfig = [[${codeConfig}]];
        form.val('codeConfigForm', codeConfig);
        $.ajax({
            url: ctx + '/code/table',
            dataType: 'json',
            type: 'get',
            success: function (result) {
                if (result.code === 500) {
                    layer.msg(result.message);
                    return;
                }
                let data = result.data;
                $.each(data, function (index, value) {
                    $('#tableName').append(new Option(value,value));
                });
                layui.form.render("select");
            }
        })
        form.on('select(tableName)', function(data) {
            $.ajax({
                url: ctx + '/code/field?tableName='+data.value,
                dataType: 'json',
                type: 'get',
                success: function (result) {
                    let data = result.data;
                    table.reload('fieldList',{
                        data
                    });
                }
            })
        })
        /**
         * 字段列表
         */
        table.render({
            elem: '#fieldList'
            ,cellMinWidth: 80
            ,height: 'full-110'
            ,cols: [
                [
                    {field: 'columnName', title: '字段名'},
                    {field: 'typeName', title: '字段类型'},
                    {field: 'columnSize', title: '字段长度'},
                    {field: 'remarks', title: '描述'}
                ]
            ]
            ,data: []
            ,even: true
        });
        form.on('submit(save-submit)', function(data) {
            $.ajax({
                type: 'POST',
                url:  ctx + '/code/build',
                data: JSON.stringify(data.field),
                contentType:'application/json;charset=UTF-8',
                dataType: 'json',
                success: function(result) {
                    layer.msg(result.message);
                }
            });
        });
        // 菜单向导
        $('.menuWizard').on('click', function () {
            // 树结构向导
            crud.treeWizard("/system/sysMenu/treeSelect",function (result) {
                form.val('codeConfigForm', {
                    menuId: result.id,
                    menuName: result.name
                });
            });
        });
    });
</script>
</body>
</html>